/*
*  Impp - Instant Messenger
*
*  Copyright (C) 2008  Oliver Schneider
*
*  This program is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program; if not, write to the Free Software
*  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
#ifndef OSCAR_H_
#define OSCAR_H_

#include "../../core/interfaces/iProtocol.h"
#include "OscarServerCon.h"
#include "tasks/TaskManager.h"
#include "events/PackageEvent.h"
#include "events/ConnectionEvent.h"
#include "SsiPool.h"

namespace Impp {

class OscarServerCon;
class PackageEvent;
class ConnectionEvent;
class TaskManager;

enum OscarState{
	OS_DISCONNECTED,
	OS_CONNECTING_TO_LOGIN,
	OS_CONNECTING_TO_BOS,
	OS_DISCONNECTION,
	OS_CONNECTED
};

enum LoginState{
	LOGIN_STATE_NOTINITIALIZED = 0,
	LOGIN_STATE_WAITING_FOR_LOGIN_HELLO = 1,
	LOGIN_STATE_WAITING_FOR_PW_HASH = 2,
	LOGIN_STATE_WAITING_FOR_PW_REPLAY = 3,
	LOGIN_STATE_WAITING_FOR_BOS_HELLO = 4,
	LOGIN_STATE_SETTING_STATUS_AND_PRIVACY = 5,
	LOGIN_STATE_COMPLETE = 6,
	LOGIN_STATE_ERROR = 7
};

enum{
	ID_OSCAR = 300
};

class Oscar : public iProtocol
{

	DECLARE_DYNAMIC_CLASS(Oscar)

private:
	TaskManager* taskManager;
	OscarServerCon* login;
	OscarServerCon* bos;
	OscarServerCon* service;
	SsiPool* ssi;
	Account* account;
	OscarState state;

	LoginState loginState;
	Status statusToSet;
	PrivacySetting visibilityToSet;
	word metaSeqNr;
	bool serviceConRequested;

public:
	Oscar();
	virtual ~Oscar();

	void init(Account* account);
	void sendMessage(ActionId id, Buddy* & buddy,const wxString & msg);
	void sendTypingNotification(Buddy* buddy, TypingState state);
	void connect(const ActionId & id, const Status & status, const wxString & statusMsg, const PrivacySetting & visibility);
	void setStatus(const ActionId & id, const Status & status, const wxString & statusMsg = wxEmptyString);
	void setPrivacySettings(const PrivacySetting & visibility);

	void disconnect();
	ConnectionState getConnectionState();

	void changeCustomName(Buddy* buddy,const wxString & newCustomName);
	void changeBuddyGroup(Buddy* buddy,Group* newGroup);
	void changeAddToPrivacyList(Buddy* buddy, const int & privacyList);
	void changeRemoveFromPrivacyList(Buddy* buddy, const int & privacyList);
	void changeOwnAvatar(const wxString & avatarFile);

	void requestStatusMessage(Buddy* buddy);
	void requestAvatar(const ActionId & id, Buddy* buddy);
	void replayAuthorisation(Buddy* buddy, bool accept,const wxString & reason) ;
	void checkUserStatus(Buddy* buddy);

	void onPackageIn(PackageEvent & event);
	void onConnectionEvent(ConnectionEvent & event);
	void statusMessageRequestReceived(Buddy* buddy, byte msgType, qword cookie);
	void IncomingFileTransfareRequestReceived(Buddy* buddy, qword cookie, const wxString & fileName, word fileCount, dword totalSize, const FileTransfareInfo & info);
	void replayFileTransfare(const ActionId & id, Buddy* buddy, bool accept);

	void requestShortUserInfo(Buddy* buddy);
	void requestFullUserInfo(Buddy* buddy);
	TaskManager* getTaskManager();
	SsiPool* getSsi();

	OscarServerCon* getServiceConnection();
	OscarServerCon* getBosConnection();
	void requestServiceConnection();
	bool serviceConnectionRequested();
	void serviceRequestTimedOut();
	qword createCookie();
	Account* getAccount();
	PrivacySetting getPrivacySettings();

	Status getSupportedStatusMask();
	int getExtraStatusCount();
	void setExtraStatus(int nr, Status & status);
	wxString getExtraStatusName(int nr);

private:
	void parseLoginPackage(Flap* flap);
	void parseBosPackage(Flap* flap);
	void parseServicePackage(Flap* flap);

private:
   // DECLARE_EVENT_TABLE()

};

}

#endif /*OSCAR_H_*/
